//
//  ActionTo.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/5/24.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit

private struct AssociateKeys {
    static var infoKey: Void?
}

// MARK: - cell 默认实现的协议 实现默认方法
public protocol BindToProtocol : NSObjectProtocol {
    func bindTo(_ info: Any?) -> Void
}

public extension BindToProtocol {
    var info: Any? {
        get {
            return objc_getAssociatedObject(self, &AssociateKeys.infoKey)
        }
        set {
            objc_setAssociatedObject(self, &AssociateKeys.infoKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

// MARK: - UITableViewCell
extension UITableViewCell: BindToProtocol {

    @objc open func bindTo(_ info: Any?) {
        guard let info = info else { return }
        self.info = info
        guard let model = info as? TableCellConfigProtocol else { return }
        if !model.isCellConfigDone {
            model.isCellConfigDone = true
            model.cellOnce(self)
        }
        model.cellEvery(self)
    }

}

// MARK: - UITableViewCell
extension UICollectionViewCell: BindToProtocol {
    
    @objc open func bindTo(_ info: Any?) {
        guard let info = info else { return }
        self.info = info
        guard let model = info as? CollectionCellConfigProtocol else { return }
        if !model.isCellConfigDone {
            model.isCellConfigDone = true
            model.cellOnce(self)
        }
        model.cellEvery(self)
    }
    
}
